#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
#include "mg_file.h"
/* widgets:
Layer_ScreenSaver
ScreenSaver_Background
Container_Video
Video_ScreenSaver
Container_DigitalClock
DigitalClock
Text_Minute
Text_Hour
Container_AnalogClock
Icon_Center
AnalogClock
Icon_MinuteHand
Icon_HourHand
DigitalClock1
Text_Day
Button_Back_MainMenu
*/
#define SCREENSAVER_TYPE_NUM			14
//更具配置参数显示屏保
void Screensaver_Update_By_Sys(uint8_t ScreensaverMode);
//根据屏保参数获取Bit位位置
uint16_t GetBitFromScreensaver(uint8_t Screensaver_Config);
//获取下一个屏保
uint8_t ScreensaverNext(uint8_t Screensaver_Config);
//获取上一个屏保
uint8_t ScreensaverPrev(uint8_t Screensaver_Config);

static ITUBackground* ScreenSaver_Background;
static ITUAnalogClock* AnalogClock;
static ITUAnalogClock* DigitalClock;
static ITUContainer* Container_AnalogClock;
static ITUContainer* Container_DigitalClock;
static ITUContainer* Container_Video1, *Container_Video2;
static ITUContainer* Container_MoorgenLogo;
static ITUContainer* Container_Surname;
static ITUContainer* Container_Photo[PICTURE_MAX_NUM];
static ITUIcon* Icon_UserPhoto[PICTURE_MAX_NUM];
static ITUVideo* Video_ScreenSaver1;
static ITUText* Text_Surname;
static uint16_t scrolling_num = 1, scrollingtime = 0;//屏保滚动计时
bool Preview_Mode = false;//预览模式
uint8_t Preview_ScreenSaver = 1;//预览
static uint16_t Timer_3H = 0;//熄屏计时
static bool Lcd_Sleep_State = false;//熄屏状态
bool ScreenSaverOnEnter(ITUWidget* widget, char* param)
{
	char WidgetName[32];
	if (!ScreenSaver_Background)
	{
		ScreenSaver_Background = ituSceneFindWidget(&theScene, "ScreenSaver_Background");
		assert(ScreenSaver_Background);
		AnalogClock = ituSceneFindWidget(&theScene, "AnalogClock");
		assert(AnalogClock);
		DigitalClock = ituSceneFindWidget(&theScene, "DigitalClock");
		assert(DigitalClock);
		Container_AnalogClock = ituSceneFindWidget(&theScene, "Container_AnalogClock");
		assert(Container_AnalogClock);
		Container_DigitalClock = ituSceneFindWidget(&theScene, "Container_DigitalClock");
		assert(Container_DigitalClock);
		Container_Video1 = ituSceneFindWidget(&theScene, "Container_Video1");
		assert(Container_Video1);
		Container_Video2 = ituSceneFindWidget(&theScene, "Container_Video2");
		assert(Container_Video2);
		Container_MoorgenLogo = ituSceneFindWidget(&theScene, "Container_MoorgenLogo");
		assert(Container_MoorgenLogo);
		Container_Surname = ituSceneFindWidget(&theScene, "Container_Surname");
		assert(Container_Surname);
		for (uint8_t i = 0; i < PICTURE_MAX_NUM; i++)
		{
			sprintf(WidgetName, "Container_Photo%d", i);
			Container_Photo[i] = ituSceneFindWidget(&theScene, WidgetName);
			assert(Container_Photo[i]);
			sprintf(WidgetName, "Icon_UserPhoto%d", i);
			Icon_UserPhoto[i] = ituSceneFindWidget(&theScene, WidgetName);
			assert(Icon_UserPhoto[i]);
		}
		Video_ScreenSaver1 = ituSceneFindWidget(&theScene, "Video_ScreenSaver1");
		assert(Video_ScreenSaver1);
		Text_Surname = ituSceneFindWidget(&theScene, "Text_Surname");
		assert(Text_Surname);
	}
	//对时
	MessageFlag[MSG_ENTER_SCREENSAVER] = ON;//打开进入屏保标志
	scrollingtime = 0;
	Timer_3H = 0;
	Clear_User_Action();//进入界面后清空所有操作
	if (MessageFlag[MSG_SCREENSAVER_PREVIEW] == ON)//预览模式
	{
		MessageFlag[MSG_SCREENSAVER_PREVIEW] = OFF;
		Preview_Mode = true;
		Preview_ScreenSaver = Sys_Config.byte.Null;
		if ((Preview_ScreenSaver != 0 && Preview_ScreenSaver <= SCREENSAVER_TYPE_NUM) ||
			Preview_ScreenSaver == 0xAA)
			Screensaver_Update_By_Sys(Preview_ScreenSaver);
		else
			Screensaver_Update_By_Sys(Sys_Config.byte.Screensaver_Type);
	}
	else
	{
		Screensaver_Update_By_Sys(Sys_Config.byte.Screensaver_Type);
	}
	return true;
}

bool ScreenSaverOnLeave(ITUWidget* widget, char* param)
{
	MessageFlag[MSG_ENTER_SCREENSAVER] = OFF;
	scrollingtime = 0xFFFF;//滚动屏保计时失效
	Preview_Mode = false;//预览模式失效
	Preview_ScreenSaver = 1;
	Timer_3H = 0xFFFF;//熄屏失效
	if (Dev.Lcd_Is_Sleep)APP_Lcd_Resume();
	return true;
}

bool ScreenSaverOnTimer(ITUWidget* widget, char* param)
{
	static bool SleepMode = false;

	if (Action_Cmd.User_Action != Null_Action)
	{
		printf("Moorgen INFO:Goto Mainmenu by rotate or gesture.\n");
		Clear_User_Action();
		GotoMainMenu();
	}
	if (Sys_Config.byte.Screensaver_En == 0x01)//进入休眠模式前停止播放MP4
	{
#ifndef CFG_GPIO_EXT_WATCHDOG
		SleepMode = true;
		printf("Moorgen INFO:Stop Display mp4.\n");
		if (Sys_Config.byte.Screensaver_Type == 0x04 || 
			Sys_Config.byte.Screensaver_Type == 0x05 ||
			Sys_Config.byte.Screensaver_Type == 0x07 ||
			Sys_Config.byte.Screensaver_Type == 0x08 ||
			Sys_Config.byte.Screensaver_Type == 0x09)
			ituVideoPlay(Video_ScreenSaver1, 0);
#endif
	}
	else//退出休眠模式后播放MP4
	{
		if (SleepMode == true)
		{
			SleepMode = false;
			printf("Moorgen INFO:Replay mp4.\n");
			if (Sys_Config.byte.Screensaver_Type == 0x04 ||
				Sys_Config.byte.Screensaver_Type == 0x05 ||
				Sys_Config.byte.Screensaver_Type == 0x07 ||
				Sys_Config.byte.Screensaver_Type == 0x08 ||
				Sys_Config.byte.Screensaver_Type == 0x09)
				ituVideoPlay(Video_ScreenSaver1,0);
		}
	}
	if (MessageFlag[MSG_SCREENSAVER_CUT] == ON)
	{
		MessageFlag[MSG_SCREENSAVER_CUT] = OFF;
		if ((Preview_Mode == false && Sys_Config.byte.Screensaver_Type == 0xAA) ||
			(Preview_Mode == true && Preview_ScreenSaver == 0xAA))//滚动显示屏保
		{
			do
			{
				scrolling_num = ScreensaverNext(scrolling_num);
				printf("Moorgen INFO : Switch to the next screen saver, scrolling_num = %d Screen saver Num %d Enable:%d\n", scrolling_num, scrolling_num, ((Sys_Config.byte.ScreensaverLoop_Select >> GetBitFromScreensaver(scrolling_num)) & 0x01));
			} while (((Sys_Config.byte.ScreensaverLoop_Select >> GetBitFromScreensaver(scrolling_num)) & 0x01) == 0);
			Screensaver_Update_By_Sys(scrolling_num);
		}
	}

	if (MessageFlag[MSG_SCREENSAVER_PREVIEW] == ON)//预览模式
	{
		MessageFlag[MSG_SCREENSAVER_PREVIEW] = OFF;
		Preview_Mode = true;
		scrollingtime = 0;
		Preview_ScreenSaver = Sys_Config.byte.Null;
		if ((Preview_ScreenSaver != 0 && Preview_ScreenSaver <= SCREENSAVER_TYPE_NUM) ||
			Preview_ScreenSaver == 0xAA)
			Screensaver_Update_By_Sys(Preview_ScreenSaver);
		else
			Screensaver_Update_By_Sys(Sys_Config.byte.Screensaver_Type);
	}
	else if (MessageFlag[MSG_GUI_SWITCH_SCREENSAVER] == ON)
	{
		Screensaver_Update_By_Sys(Sys_Config.byte.Screensaver_Type);//显示系统配置屏保
		MessageFlag[MSG_GUI_SWITCH_SCREENSAVER] = OFF;
	}
	else if (MessageFlag[MSG_GUI_SWITCH_SURNAME] == ON && Sys_Config.byte.Screensaver_Type == 0x06)
	{
		Screensaver_Update_By_Sys(Sys_Config.byte.Screensaver_Type);//显示系统配置姓氏
		MessageFlag[MSG_GUI_SWITCH_SURNAME] = OFF;
	}
	if (MessageFlag[MSG_LCD_SLEEP])
	{
		MessageFlag[MSG_LCD_SLEEP] = OFF;
		APP_Lcd_Sleep();
		ituWidgetSetVisible(Container_AnalogClock, false);//设置为隐藏
		ituWidgetSetVisible(Container_DigitalClock, false);//设置为隐藏
		ituWidgetSetVisible(Container_Video1, false);//设置为隐藏
		ituWidgetSetVisible(Container_Video2, false);//设置为隐藏
		ituWidgetSetVisible(Container_MoorgenLogo, false);//设置为隐藏
		ituWidgetSetVisible(Container_Surname, false);//设置为隐藏
		ituWidgetSetVisible(Container_Photo[0], false);//设置为隐藏
		ituWidgetSetVisible(Container_Photo[1], false);//设置为隐藏
		ituWidgetSetVisible(Container_Photo[2], false);//设置为隐藏
	}
    return true;
}

bool SlideRightToBackMainmenu(ITUWidget* widget, char* param)
{
	printf("Moorgen INFO:Goto Mainmenu by touch.\n");
	GotoMainMenu();
    return true;
}
//函数接口返回主菜单
void GotoMainMenu(void)
{
	ITULayer* Layer_MainMenu[6];
	Layer_MainMenu[0] = ituSceneFindWidget(&theScene, "Layer_MainMenu1");
	Layer_MainMenu[1] = ituSceneFindWidget(&theScene, "Layer_MainMenu2");
	Layer_MainMenu[2] = ituSceneFindWidget(&theScene, "Layer_MainMenu3");
	Layer_MainMenu[3] = ituSceneFindWidget(&theScene, "Layer_MainMenu4");
	for (int i = 0; i < 4; i++)
	{
		assert(Layer_MainMenu[i]);
	}
#ifdef WIN32
	ituLayerGoto(Layer_MainMenu[3]); 
#else
	//判断系统菜单类型并跳转到对应菜单
	if (Sys_Config.byte.MenuType <=6 && Sys_Config.byte.MenuType != 0)
	{
		ituLayerGoto(Layer_MainMenu[Sys_Config.byte.MenuType-1]);
	}
	else
	{
		ituLayerGoto(Layer_MainMenu[1]);
	}
	printf("Moorgen INFO:Go back Mainmenu %d.\n", (Sys_Config.byte.MenuType - 1));
#endif
	
}
//更具配置参数显示屏保
void Screensaver_Update_By_Sys(uint8_t ScreensaverMode)
{
	uint8_t OneStringSize = 0, StringLen = 0;
	char Display_Str[12] = "";
	if (ituWidgetIsVisible(Container_AnalogClock))
		ituWidgetSetVisible(Container_AnalogClock, false);//设置为隐藏
	if (ituWidgetIsVisible(Container_DigitalClock))
		ituWidgetSetVisible(Container_DigitalClock, false);//设置为隐藏
	if (ScreensaverMode == 4 || 
		ScreensaverMode == 5 ||
		ScreensaverMode == 7 ||
		ScreensaverMode == 8 ||
		ScreensaverMode == 9 )
		ituVideoStop(Video_ScreenSaver1);//停止播放
	else
		ituWidgetSetVisible(Container_Video1, false);//设置为隐藏
	if (ituWidgetIsVisible(Container_MoorgenLogo))
		ituWidgetSetVisible(Container_MoorgenLogo, false);//设置为隐藏
	if (ituWidgetIsVisible(Container_Surname))
		ituWidgetSetVisible(Container_Surname, false);//设置为隐藏
	if (ituWidgetIsVisible(Container_Photo[0]))
		ituWidgetSetVisible(Container_Photo[0], false);//设置为隐藏
	if (ituWidgetIsVisible(Container_Photo[1]))
		ituWidgetSetVisible(Container_Photo[1], false);//设置为隐藏
	if (ituWidgetIsVisible(Container_Photo[2]))
		ituWidgetSetVisible(Container_Photo[2], false);//设置为隐藏

	if (Sys_Config.byte.Screensaver_En == 0x01)//关闭屏保
	{
		APP_Lcd_Sleep();
		return;
	}
	else
	{
		APP_Lcd_Resume();
		Timer_3H = 0;
	}
	printf("Moorgen INFO : ScreensaverMode: %02X \n", ScreensaverMode);
	if (ScreensaverMode == 0x01)//模拟时钟
	{
		printf("Moorgen INFO : Enter the analog clock screen saver.\n");
		ituAnalogClockUpdate((ITUWidget*)AnalogClock, ITU_EVENT_TIMER, 0, 0, 0);
		ituWidgetSetVisible(Container_AnalogClock, true);//设置为显示
	}
	else if (ScreensaverMode == 0x02)//数字时钟
	{
		printf("Moorgen INFO : Enter the digital clock screen saver.\n");
		ituDigitalClockUpdate((ITUWidget*)DigitalClock, ITU_EVENT_TIMER, 0, 0, 0);
		ituWidgetSetVisible(Container_DigitalClock, true);//设置为显示
	}
	else if (ScreensaverMode == 0x03)//Moorgen logo
	{
		printf("Moorgen INFO : Enter the Moorgen logo screen saver.\n");
		ituWidgetSetVisible(Container_MoorgenLogo, true);//设置为显示
	}
	else if (ScreensaverMode == 0x04)//蝴蝶视频
	{
		printf("Moorgen INFO : Enter the butterfly screen saver.\n");
		ituWidgetSetVisible(Container_Video1, true);//设置为显示
		strcpy(Video_ScreenSaver1->filePath, "B:/media/ScreenSaver1.mp4");
		ituVideoPlay(Video_ScreenSaver1, 0);//开始播放
	}
	else if (ScreensaverMode == 0x05)//鲜花视频
	{
		printf("Moorgen INFO : Enter the flower screen saver.\n");
		ituWidgetSetVisible(Container_Video1, true);//设置为显示
		strcpy(Video_ScreenSaver1->filePath, "B:/media/ScreenSaver2.mp4");
		ituVideoPlay(Video_ScreenSaver1, 0);//开始播放
	}
	else if (ScreensaverMode == 0x06)//姓氏界面
	{
		memcpy(Display_Str, Sys_Config.byte.Surname, sizeof(Sys_Config.byte.Surname));
		printf("Moorgen INFO : Enter the surname screen saver.\n");
		ituTextSetString(Text_Surname, Display_Str);
		OneStringSize = Get_UTF8_Byte_Size(Sys_Config.byte.Surname[0]);//单个字符长度
		StringLen = Get_UTF8_Str_Size(Sys_Config.byte.Surname, sizeof(Sys_Config.byte.Surname));//字符个数
		//字体大小根据字符长短设置
		if (OneStringSize == 3)//中文字符
		{
			Text_Surname->fontIndex = 6;
			ituWidgetSetY(Text_Surname, 14);
			if (StringLen == 1)//一个中文
			{
				ituWidgetSetY(Text_Surname, 14);
				ituTextSetFontSize(Text_Surname, 150);
			}
			else if (StringLen == 2)//两个中文
			{
				ituWidgetSetY(Text_Surname, 10);
				ituTextSetFontSize(Text_Surname, 90);
			}
			else//三个或三个以上中文
			{
				ituWidgetSetY(Text_Surname, 10);
				ituTextSetFontSize(Text_Surname, 60);
			}
		}
		else//英文
		{
			Text_Surname->fontIndex = 5;
			if (StringLen <= 3)
			{
				ituWidgetSetY(Text_Surname, 5);
				ituTextSetFontSize(Text_Surname, 80);
			}
			else if (StringLen <= 5)
			{
				ituWidgetSetY(Text_Surname, 5);
				ituTextSetFontSize(Text_Surname, 68);
			}
			else if (StringLen <= 7)
			{
				ituWidgetSetY(Text_Surname, 5);
				ituTextSetFontSize(Text_Surname, 56);
			}
			else if (StringLen <= 9)
			{
				ituWidgetSetY(Text_Surname, 5);
				ituTextSetFontSize(Text_Surname, 44);
			}
			else
			{
				ituWidgetSetY(Text_Surname, 5);
				ituTextSetFontSize(Text_Surname, 40);
			}
		}
		ituWidgetSetVisible(Container_Surname, true);//设置为显示
	}
	else if (ScreensaverMode == 0x07)//鲸鱼视频
	{
		printf("Moorgen INFO : Enter the whale screen saver.\n");
		ituWidgetSetVisible(Container_Video1, true);//设置为显示
		strcpy(Video_ScreenSaver1->filePath, "B:/media/ScreenSaver3.mp4");
		ituVideoPlay(Video_ScreenSaver1, 0);//开始播放
	}
	else if (ScreensaverMode == 0x08)//水母视频
	{
		printf("Moorgen INFO : Enter the jellyfish screen saver.\n");
		ituWidgetSetVisible(Container_Video1, true);//设置为显示
		strcpy(Video_ScreenSaver1->filePath, "B:/media/ScreenSaver4.mp4");
		ituVideoPlay(Video_ScreenSaver1, 0);//开始播放
	}
	else if (ScreensaverMode == 0x09)//小鹿视频
	{
		printf("Moorgen INFO : Enter the deer screen saver.\n");
		ituWidgetSetVisible(Container_Video1, true);//设置为显示
		strcpy(Video_ScreenSaver1->filePath, "B:/media/ScreenSaver5.mp4");
		ituVideoPlay(Video_ScreenSaver1, 0);//开始播放
	}
	else if (ScreensaverMode == 0x0A)//用户自定义照片1
	{
		printf("Moorgen INFO : Enter the User Phone1 screen saver.\n");
		ituWidgetSetVisible(Container_Photo[0], true);//设置为显示(当容器为隐藏时，加载PNG数据会无效)
#ifndef _WIN32
		if (Photo_File[0].File_Size)
			ituIconLoadPngData(Icon_UserPhoto[0], Photo_File[0].File_Data, Photo_File[0].File_Size);
#endif
		ituWidgetSetVisible(Icon_UserPhoto[0], true);//设置为显示
	}
	else if (ScreensaverMode == 0x0B)//用户自定义照片2
	{
		printf("Moorgen INFO : Enter the User Phone2 screen saver.\n");
		ituWidgetSetVisible(Container_Photo[1], true);//设置为显示(当容器为隐藏时，加载PNG数据会无效)
#ifndef _WIN32
		if (Photo_File[1].File_Size)
			ituIconLoadPngData(Icon_UserPhoto[1], Photo_File[1].File_Data, Photo_File[1].File_Size);
#endif
		ituWidgetSetVisible(Icon_UserPhoto[1], true);//设置为显示
	}
	else if (ScreensaverMode == 0x0C)//用户自定义照片3
	{
		printf("Moorgen INFO : Enter the User Phone3 screen saver.\n");
		ituWidgetSetVisible(Container_Photo[2], true);//设置为显示(当容器为隐藏时，加载PNG数据会无效)
#ifndef _WIN32
		if (Photo_File[2].File_Size)
			ituIconLoadPngData(Icon_UserPhoto[2], Photo_File[2].File_Data, Photo_File[2].File_Size);
#endif
		ituWidgetSetVisible(Icon_UserPhoto[2], true);//设置为显示
	}
	else if (ScreensaverMode == 0xAA)//滚动显示
	{
		printf("Moorgen INFO : Enter the scrolling display screen saver.\n");
		ituWidgetSetVisible(Container_AnalogClock, true);//设置为显示
		Screensaver_Update_By_Sys(scrolling_num);//回调一次
	}
	else
	{
		printf("Moorgen INFO : Enter the analog clock screen saver.\n");
		ituWidgetSetVisible(Container_AnalogClock, true);//设置为显示
	}
}
//屏保轮询进程
void Screensaver_Loop_Pro(void)
{
	if (scrollingtime == 0xFFFF)return;
	if ((Preview_Mode == false && Sys_Config.byte.Screensaver_Type == 0xAA) ||
		(Preview_Mode && Preview_ScreenSaver == 0xAA))//滚动显示所有屏保
	{
		if (scrollingtime < Sys_Config.byte.ScrollingTime)
			scrollingtime++;
		else
		{
			scrollingtime = 0;
			MessageFlag[MSG_SCREENSAVER_CUT] = ON;
		}
	}
}
//获取下一个屏保
uint8_t ScreensaverNext(uint8_t Screensaver_Config)
{
	uint8_t Ret_Screensaver = 0;
	if (Screensaver_Config < SCREENSAVER_TYPE_NUM)
		Ret_Screensaver = Screensaver_Config+1;
	else if (Screensaver_Config == SCREENSAVER_TYPE_NUM)
		Ret_Screensaver = 1;
	return Ret_Screensaver;
}
//获取下一个屏保
uint8_t ScreensaverPrev(uint8_t Screensaver_Config)
{
	uint8_t Ret_Screensaver = 0;

	if (Screensaver_Config == 0x01)
		Ret_Screensaver = SCREENSAVER_TYPE_NUM;
	else if (Screensaver_Config > 1 && Screensaver_Config <= SCREENSAVER_TYPE_NUM)
		Ret_Screensaver = Screensaver_Config--;
	return Ret_Screensaver;

}
//根据屏保参数获取Bit位位置
uint16_t GetBitFromScreensaver(uint8_t Screensaver_Config)
{
	uint16_t Bit_Count = 0;
	if (Screensaver_Config != 0 && Screensaver_Config <= SCREENSAVER_TYPE_NUM)
		Bit_Count = Screensaver_Config - 1;
	return Bit_Count;
}

//LCD强制关闭倒计时
void LCD_Close_Pro(void)
{
	if (Timer_3H == 0xFFFF)return;
	if (Preview_Mode == false)//不在预览模式下
	{
		if (Timer_3H < 10800)//3小时（10800s）
			Timer_3H++;
		else if (Timer_3H == 10800)
		{
			Timer_3H = 0xFFFF;
			MessageFlag[MSG_LCD_SLEEP] = ON;
		}
	}
}
//1s定时
void Screensaver_Timer(void)
{
	Screensaver_Loop_Pro();
	LCD_Close_Pro();
}
